/*
 * stub.c
 *
 *  Created on: Jun 9, 2010
 *      Author: iandrono
 */

#include <avr/io.h>
#include "stub.h"
#include "../bluetooth/bluetooth.h"
#include "../defines.h"
#include "../leds/leds.h"
#include "../memoryManager/memoryManager.h"
#include "../uart/uart.h"
#include "../timer/timer.h"

void staticImageHandler( void ) {
	//Leo los 6 bytes de data y el checksum, el 1er oper ya lo leyeron
	imageType leds;
	unsigned char data[COLORS_Q_MAX*ROWS_Q*(LEDS_Q_MAX/BYTE_SIZE)];
	unsigned char checksum;
	int i, j, k, p;
	operationType oper;
	booleanType receiving;

	oper = STATIC_IMAGE;
	receiving = TRUE;
	j=0;
	do {
		for( i=0 ; i<PACKET_SIZE-2 ; i++ ) {
			//Leo (-2) porque ya consumi el byte de operacion y el
			data[j] = readUart();
			j++;
		}
		checksum = readUart();

		//TODO:Valido los 6 bytes de data con checksum

		if( oper == END_DATA )
			receiving = FALSE;
		else
			oper = readUart();
	} while( receiving );

	//Paso la data a una imageType
	p = 0;
	for( i=0 ; i<COLORS_Q_MAX ; i++ ) {
		for( j=0 ; j<ROWS_Q ; j++ ) {
			for( k=0 ; k<LEDS_Q_MAX/BYTE_SIZE ; k++ ) {
				leds[i][j][k] = data[p++];
			}
		}
	}
	resetVideoMemory(&leds);
}

void setTimerCounterHandler( void ) {
	//Leo los 6 bytes de data y el checksum, el 1er oper ya lo leyeron
	unsigned char data[PACKET_SIZE-1];
	int i;

	for( i=0 ; i<PACKET_SIZE-1 ; i++ ) {
		data[i] = readUart();
	}

	setTimerCounter(data[0]);
}

void setRowsQHandler( void ) {
	//Leo los 6 bytes de data y el checksum, el 1er oper ya lo leyeron
	unsigned char data[PACKET_SIZE-1];
	int i;

	for( i=0 ; i<PACKET_SIZE-1 ; i++ ) {
		data[i] = readUart();
	}
	ROWS_Q = data[0];
}
